package com.shiva.oms.logview.api.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.sf.shiva.oms.common.util.BeanCopyUtil;
import com.shiva.oms.biz.CustomConnectCfgBiz;
import com.shiva.oms.logview.domain.ConnectCfg;
import com.shiva.oms.logview.util.SshTransferHelper;
import java.awt.Font;


/**
 * 
 * 
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年4月16日         LA.01367604           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author LA.01367604
 * @version 1.0
 */
public class NewConnectCfgGui extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;

    private CustomConnectCfgBiz customCfgBiz = CustomConnectCfgBiz.getInstance();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            NewConnectCfgGui dialog = new NewConnectCfgGui();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public NewConnectCfgGui() {
        setResizable(false);
        initialize(null);
    }

    public NewConnectCfgGui(JFrame parent, ConnectCfg connectCfg) {
        super(parent, true);
        initialize(connectCfg);
    }

    public NewConnectCfgGui(JFrame parent) {
        super(parent, true);
        initialize(null);
    }

    public NewConnectCfgGui(JDialog parent) {
        super(parent, true);
        initialize(null);
    }
    
    public NewConnectCfgGui(ConnectCfg connectCfg) {
        initialize(connectCfg);
    }

    /**
     * Create the dialog.
     */
    private void initialize(ConnectCfg connectCfg) {
        setTitle("新建连接");
        setBounds(100, 100, 380, 342);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        {
            JLabel lblip = new JLabel("主机IP：");
            lblip.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            lblip.setBounds(10, 50, 54, 15);
            contentPanel.add(lblip);
            textField = new JTextField();
            textField.setBounds(79, 47, 154, 21);
            contentPanel.add(textField);
            textField.setColumns(10);
        }
        {
            JLabel label = new JLabel("用户名：");
            label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            label.setBounds(10, 93, 54, 15);
            contentPanel.add(label);
            textField_1 = new JTextField();
            textField_1.setBounds(79, 90, 154, 21);
            contentPanel.add(textField_1);
            textField_1.setColumns(10);
        }
        {
            JLabel label = new JLabel("密码：");
            label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            label.setBounds(10, 132, 54, 15);
            contentPanel.add(label);
            textField_2 = new JTextField();
            textField_2.setBounds(79, 129, 154, 21);
            contentPanel.add(textField_2);
            textField_2.setColumns(10);
        }
        {
            JLabel label = new JLabel("文件存放目录：");
            label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            label.setBounds(10, 217, 85, 15);
            contentPanel.add(label);
            textField_3 = new JTextField();
            textField_3.setBounds(99, 214, 258, 21);
            contentPanel.add(textField_3);
            textField_3.setColumns(10);
        }
        {
            JButton btnNewButton = new JButton("测试连接");
            btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    long startTime = System.currentTimeMillis(); // 获取开始时间
                    String msg = "登录成功";
                    SshTransferHelper ssh = new SshTransferHelper();
                    if (!ssh.connectHost(textField.getText(), textField_1.getText(), textField_2.getText())) {
                        msg = "登录失败";
                    }
                    JOptionPane.showMessageDialog(contentPanel, msg, "测试连接", JOptionPane.WARNING_MESSAGE);
                    long endTime = System.currentTimeMillis(); // 获取结束时间
                    System.out.println("连接host获取文件的后台调用运行时间：" + (endTime - startTime) + "ms"); // 输出程序运行时间
                }
            });
            btnNewButton.setBounds(78, 160, 93, 23);
            contentPanel.add(btnNewButton);
        }
        if (connectCfg != null) { // 直接连接
            JButton button = new JButton("连接");
            button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    ConnectCfg newConnectCfg = createConnectCfg();
                    if (isConnectSucess(newConnectCfg)) { // 判断是否能够连接成功，如果连接登录成功则保存到本地的临时文件中
                        BeanCopyUtil.copyProperties(connectCfg, newConnectCfg, false);
                        saveCfg2File();
                        setVisible(false);
                    } else {

                    }
                }
            });
            button.setBounds(120, 260, 63, 23);
            contentPanel.add(button);
            button.setActionCommand("OK");
        } else { // 返回保存
            JButton okButton = new JButton("保存");
            okButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            okButton.setBounds(120, 260, 63, 23);
            contentPanel.add(okButton);
            okButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    saveCfg2File();
                    setVisible(false);
                }
            });
            okButton.setActionCommand("OK");
            getRootPane().setDefaultButton(okButton);
        }
        JButton cancelButton = new JButton("取消");
        cancelButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        cancelButton.setBounds(200, 260, 63, 23);
        contentPanel.add(cancelButton);
        cancelButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
        cancelButton.setActionCommand("Cancel");
    }

    /**
     * 保存连接配置到本地文件，供历史记录查询和连接配置管理使用
     * 
     * @author LA.01367604-2018年5月7日
     */
    private void saveCfg2File() {
        ConnectCfg cfg = createConnectCfg();
        customCfgBiz.addCfg(cfg);
    }

    private ConnectCfg createConnectCfg() {
        String ip = textField.getText();
        String user = textField_1.getText();
        String password = textField_2.getText();
        String dir = textField_3.getText();
        String cfgName = ip + "@" + user;
        ConnectCfg cfg = new ConnectCfg(cfgName, ip, user, password, dir);
        return cfg;
    }

    private boolean isConnectSucess(ConnectCfg cfg) {
        SshTransferHelper ssh = new SshTransferHelper();
        if (ssh.connectHost(cfg.getIp(), cfg.getUser(), cfg.getPassword())) {
            ssh.closeConnect();
            return true;
        }
        return false;
    }
}
